#!/usr/bin/perl
use strict;
use warnings;

use lib '../lib';
use lib '../extlib';
use ChessLike::Board;
use ChessLike::GamePiece;

use Test::Exception;
use Test::More 'no_plan';

# Create a brand new game piece with default values
my $gamepiece = ChessLike::GamePiece->new();

is( ref $gamepiece, 'ChessLike::GamePiece', '[ref] ChessLike::GamePiece' );
is( $gamepiece->name(), 'Nameless', '[new] name: default' );
is( @{ $gamepiece->move_vectors() }, 0, '[new] move_vectors: default' );
is( @{ $gamepiece->shot_vectors() }, 0, '[new] shot_vectors: default' );
is( $gamepiece->requires_units(),   undef, '[new] requires_units: default' );
is( $gamepiece->carries_units(),    undef, '[new] carries_units: default' );
is( $gamepiece->is_flying(),         0, '[new] is_flying: default' );
is( $gamepiece->can_rotate(),        0, '[new] can_rotate: default' );
is( $gamepiece->proxy_move_field(), undef, '[new] proxy_move_field: default' );
is( $gamepiece->proxy_shot_field(), undef, '[new] proxy_shot_field: default' );

# Re-create the game piece with custom values
$gamepiece = ChessLike::GamePiece->new(
   'name'             => 'Crews',
   'move-vectors'     => [ [ 1, 1, 1 ], [ 1, 2, 1 ] ],
   'shot-vectors'     => [ [ 1, 1, 5 ] ],
   'proxy-move-field' => [ 3, 3, 2, 2 ],
   'proxy-shot-field' => [ 9, 8, 1, 3 ],
   'requires-units'   => 'Xtacles',
   'carries-units'    => 'Sta-an',
   'is-flying'        => 'yes',
   'can-rotate'       => 1,
);

is( $gamepiece->name(),              'Crews',   '[new] name: custom' );
is( @{ $gamepiece->move_vectors() }, 2,         '[new] move_vectors: custom' );
is( @{ $gamepiece->shot_vectors() }, 1,         '[new] shot_vectors: custom' );
is( $gamepiece->requires_units(),   'Xtacles', '[new] requires_units: custom' );
is( $gamepiece->carries_units(),    'Sta-an',  '[new] carries_units: custom' );
is( $gamepiece->is_flying(),         1,         '[new] is_flying: custom' );
is( $gamepiece->can_rotate(),        1,         '[new] can_rotate: custom' );

my @field = @{ $gamepiece->proxy_move_field() };
is( @field   , 4, '[new] proxy_move_field: custom' );
is( $field[0], 3, '[new] proxy_move_field: custom' );
is( $field[1], 3, '[new] proxy_move_field: custom' );
is( $field[2], 2, '[new] proxy_move_field: custom' );
is( $field[3], 2, '[new] proxy_move_field: custom' );
@field = @{ $gamepiece->proxy_shot_field() };
is( @field   , 4, '[new] proxy_shot_field: custom' );
is( $field[0], 9, '[new] proxy_shot_field: custom' );
is( $field[1], 8, '[new] proxy_shot_field: custom' );
is( $field[2], 1, '[new] proxy_shot_field: custom' );
is( $field[3], 3, '[new] proxy_shot_field: custom' );

my $gamepiece2 = ChessLike::GamePiece->new();
my $gamepiece3 = ChessLike::GamePiece->new();

$gamepiece->configure(
   'name'             => 'Wendell',
   'move-vectors'     => [ [ 4, 4, 4 ], ],
   'is-flying'        => 0,
   'proxy-move-field' => undef,
   'requires-units'   => [ $gamepiece2, $gamepiece3 ],
);

is( $gamepiece->name(), 'Wendell', '[configure] name: changed' );
is( @{ $gamepiece->move_vectors() }, 1, '[configure] move_vectors: changed' );
is( @{ $gamepiece->shot_vectors() },
   1, '[configure] shot_vectors: unchanged' );
is( @{ $gamepiece->requires_units() }, 2,
   '[configure] requires_units: changed' );
is( $gamepiece->requires_units()->[0], $gamepiece2,
   '[configure] requires_units: changed' );
is( $gamepiece->requires_units()->[1], $gamepiece3,
   '[configure] requires_units: changed' );
is( $gamepiece->carries_units(), 'Sta-an',
   '[configure] carries_units: unchanged' );
is( $gamepiece->is_flying(),  0, '[configure] is_flying: changed' );
is( $gamepiece->can_rotate(), 1, '[configure] can_rotate: unchanged' );
is( $gamepiece->proxy_move_field(), undef ,
   '[configure] proxy_move_field: changed (undef)' );

@field = @{ $gamepiece->proxy_shot_field() };
is( @field,    4, '[configure] proxy_shot_field: unchanged' );
is( $field[0], 9, '[configure] proxy_shot_field: unchanged' );
is( $field[1], 8, '[configure] proxy_shot_field: unchanged' );
is( $field[2], 1, '[configure] proxy_shot_field: unchanged' );
is( $field[3], 3, '[configure] proxy_shot_field: unchanged' );

# Re-create the game piece from a config file
$gamepiece = ChessLike::GamePiece->parse( 'sample_files/weird_pawn.cfg' );

is( $gamepiece->name(), 'Weird Pawn', '[parse] name: single entry' );
is( @{ $gamepiece->move_vectors() },
   4, '[parse] move_vectors: multiple entries' );
is( @{ $gamepiece->shot_vectors() },
   3, '[parse] shot_vectors: multiple entries' );
is( $gamepiece->requires_units(),  'Weird Beacon',
   '[parse] requires_units: single entry' );
is( $gamepiece->carries_units(),  'Weirdling',
   '[parse] carries_units: multiple entries' );
is( $gamepiece->is_flying(),  0, '[parse] is_flying: single entry' );
is( $gamepiece->can_rotate(), 1, '[parse] can_rotate: multiple entries' );

# Check the contents of move-vectors
my @move_vectors = $gamepiece->move_vectors();
is( $move_vectors[0]->[0], 1, '[parse] move-vector: comma delimited' );
is( $move_vectors[0]->[1], 2, '[parse] move-vector: comma delimited' );
is( $move_vectors[0]->[2], 3, '[parse] move-vector: comma delimited' );
is( $move_vectors[1]->[0], 2, '[parse] move-vector: square brackets' );
is( $move_vectors[1]->[1], 3, '[parse] move-vector: square brackets' );
is( $move_vectors[1]->[2], 4, '[parse] move-vector: square brackets' );
is( $move_vectors[2]->[0], 3, '[parse] move-vector: standard brackets' );
is( $move_vectors[2]->[1], 4, '[parse] move-vector: standard brackets' );
is( $move_vectors[2]->[2], 5, '[parse] move-vector: standard brackets' );
is( $move_vectors[3]->[0], 4, '[parse] move-vector: space delimited' );
is( $move_vectors[3]->[1], 5, '[parse] move-vector: space delimited' );
is( $move_vectors[3]->[2], 6, '[parse] move-vector: space delimited' );

# Check the contents of shot-vectors
my @shot_vectors = $gamepiece->shot_vectors();
is( $shot_vectors[0]->[0], 9,     '[parse] shot-vector: comma delimited' );
is( $shot_vectors[0]->[1], 9,     '[parse] shot-vector: comma delimited' );
is( $shot_vectors[0]->[2], 9,     '[parse] shot-vector: comma delimited' );
is( $shot_vectors[1]->[0], 88,    '[parse] shot-vector: double-digit' );
is( $shot_vectors[1]->[1], 87,    '[parse] shot-vector: double-digit' );
is( $shot_vectors[1]->[2], 86,    '[parse] shot-vector: double-digit' );
is( $shot_vectors[2]->[0], 7,     '[parse] shot-vector: comma delimited' );
is( $shot_vectors[2]->[1], 77777, '[parse] shot-vector: multiple digits' );
is( $shot_vectors[2]->[2], 7,     '[parse] shot-vector: comma delimited' );

@field = @{ $gamepiece->proxy_move_field() };
is( @field, 4, '[parse] proxy_move_field: square brackets, space delimited' );
is( $field[0], 1,
   '[parse] proxy_move_field: square brackets, space delimited' );
is( $field[1], 2,
   '[parse] proxy_move_field: square brackets, space delimited' );
is( $field[2], 3,
   '[parse] proxy_move_field: square brackets, space delimited' );
is( $field[3], 4,
   '[parse] proxy_move_field: square brackets, space delimited' );

@field = @{ $gamepiece->proxy_shot_field() };
is( @field, 4, '[parse] proxy_shot_field: comma/space delimited' );
is( $field[0], 4,
   '[parse] proxy_shot_field: comma/space delimited' );
is( $field[1], 6,
   '[parse] proxy_shot_field: comma/space delimited' );
is( $field[2], 9,
   '[parse] proxy_shot_field: comma/space delimited' );
is( $field[3], 1,
   '[parse] proxy_shot_field: comma/space delimited' );
   
